#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int N=1e5+10;

int h[N];
int st[N];

int dist[N];

int dfs(int u,int fa)
{
	if(u==fa)
		return 1;
	st[u]=1;
	if(!st[h[u]])
		return dfs(h[u],fa)+1;
	
}
int main()
{
	int n;
	cin>>n;
	memset(h,0,sizeof(h));
	vector<int> v;
	for(int i=1;i<=n;i++)
	{
		int t;
		scanf("%d",&t);
		h[i]=t;
		v.push_back(t);
	}
	int ret=0;
	for(int i=0;i<v.size();i++)
	{
		memset(st,0,sizeof(st));
		ret=max(ret,dfs(h[v[i]],v[i]));
	}
	cout<<ret<<endl;
	return 0;
}
